package edu.lingnan.rili.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import edu.lingnan.rili.pojo.Marks;
import edu.lingnan.rili.service.MarksService;
import edu.lingnan.rili.utils.DownloadUtils;
import edu.lingnan.rili.utils.Result;
import edu.lingnan.rili.vo.AwardedVo;
import edu.lingnan.rili.vo.MarksVo;
import edu.lingnan.rili.vo.PagingQueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import static edu.lingnan.rili.controller.AdministratorController.isAdminLogin;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈世杰
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/marks")
@Api(tags = "加减分控制器")
public class MarksController {
    /** 加分标识 */
    public static final Integer INCREASE_SCORE = 1;
    /** 扣分标识 */
    public static final Integer DEDUCT__SCORE = 0;

    @Autowired
    private MarksService marksService;

    @ApiOperation("分页查询加分并且已经公示的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="currentPage",value="当前页数", required=true, paramType="query"),
            @ApiImplicitParam(name="pageSize",value="每页条数", required=true, paramType="query")
    })
    @GetMapping("/announced-page")
    public Result<PagingQueryResult<MarksVo>> getByAnnouncedAndPage(@RequestParam("currentPage") String currentPage,
                                                                    @RequestParam("pageSize") String pageSize,
                                                                    @ApiIgnore HttpSession session) {
        if ("".equals(currentPage) || "".equals(pageSize) || StringUtils.isEmpty(currentPage) ||  StringUtils.isEmpty(pageSize)) {
            return new Result<>(400, "请输入有效数据");
        }

        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }

        PagingQueryResult<MarksVo> allAnnounced = marksService.selectByAnnouncedAndPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        if (ObjectUtils.isEmpty(allAnnounced) || allAnnounced.getPageTotal() <= 0) {
            return new Result<>(201, "查询失败，未找到有效数据");
        }

        Result<PagingQueryResult<MarksVo>> result = new Result<>(200, "查询成功");
        result.setData(allAnnounced);

        return result;
    }

    @ApiOperation("导出加分表中已公示数据")
    @PostMapping("/download")
    public Result<Null> downloadExcel(@ApiIgnore HttpServletResponse response,
                                      @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }

        //构建写入到Excel中的数据
        List<MarksVo> marksVos = marksService.selectByAnnounced();
        System.out.println(marksVos);
        try {
            DownloadUtils.download(response, "加分公示栏", marksVos, MarksVo.class);
        } catch (IOException e) {
            return new Result<>(201, "表格导出错误，请联系管理人员");
        }

        return new Result<>(200, "表格导出成功");
    }

    @ApiOperation("新增分数变化数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="marksVo",value="需要新增的分数数据", required=true, paramType="body", dataType = "MarksVo"),
            @ApiImplicitParam(name="fractionFlag",value="分数变化标识，1为加分，0为减分", required=true, paramType="query")
    })
    @PostMapping("/change")
    public Result<Boolean> addScoreChange(@RequestBody MarksVo marksVo,
                                          @RequestParam("fractionFlag") Integer fractionFlag,
                                          @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        Result<Boolean> result = new Result<Boolean>(201, "新增数据失败！");
        Boolean flag = marksService.addData(marksVo, fractionFlag);
        if (flag) {
            result.setResultCode(200);
            result.setMessage("新增数据成功");
        }
        result.setData(flag);
        return result;
    }
}

